from django.db import models
from django.utils import timezone
import datetime

class Question(models.Model):
    class Meta:
        verbose_name = '要投票的问题'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.question_text
    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now
    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = '是否最近创建'
    question_text = models.CharField('问题', max_length=200)
    pub_date = models.DateTimeField('创建时间')

class Choice(models.Model):
    class Meta:
        verbose_name = '选项'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.choice_text
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
